package com.econ.powercloud.ems.timing.report.strategy.process;

import com.econ.powercloud.clickhouse.entity.EmsStatisticalMonth;
import com.econ.powercloud.ems.common.enums.ConfigColumnEnums;
import com.econ.powercloud.ems.common.enums.ConfigDitEnums;
import com.econ.powercloud.ems.common.enums.IncomeCalculationType;
import com.econ.powercloud.ems.common.util.KeepDecimalPointUtil;
import com.econ.powercloud.ems.dal.entity.EmsDevice;
import com.econ.powercloud.ems.pojo.EmsPriceInfoVO;
import com.econ.powercloud.ems.pojo.InComeCalculationVO;
import com.econ.powercloud.ems.pojo.MeterHisDataVO;
import com.econ.powercloud.ems.timing.report.strategy.AbstractIncomeCalculationProcess;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author : zjf
 * @Description : 没有特殊情况下的计算尖峰平谷的电费
 * @Date : 2022/4/5 8:51
 * @Modified by : zjf
 */
@Component
public class NormalIncomeCalculationProcess extends AbstractIncomeCalculationProcess {
    public NormalIncomeCalculationProcess() {
        setIncomeModelId(IncomeCalculationType.NORMAL.getCode());
    }

    @Override
    public InComeCalculationVO calculation(MeterHisDataVO meterHisDataVO, Map<String, EmsStatisticalMonth> timeEpMap,
                                           EmsDevice emsDevice, EmsPriceInfoVO emsPriceInfoVO, Long startDayTime) {
        Map<String,Double> dataMap = Maps.newHashMap();
        List<EmsStatisticalMonth> statisticalMonthList = Lists.newArrayList();
        //数据不全不进行计算
        if(meterHisDataVO == null || MapUtils.isEmpty(timeEpMap)){
            return InComeCalculationVO.builder().dataMap(dataMap).statisticalMonthList(statisticalMonthList).build();
        }
        //电量
        double ep = 0.0;
        String columnId = meterHisDataVO.getColumnId();
        String currentNum = meterHisDataVO.getVal();
        EmsStatisticalMonth emsStatisticalMonth = timeEpMap.get(columnId);
        //电价数据
        Double price = this.getPrice(columnId,emsPriceInfoVO);
        //添加本期示数数据
        statisticalMonthList.add(EmsStatisticalMonth.builder()
                .columnId(columnId + ConfigDitEnums.NUM_CURRENT.getCode())
                .val(currentNum)
                .time(startDayTime)
                .build());
        ep = Double.parseDouble(emsStatisticalMonth.getVal());
        //添加昨日电量数据
        statisticalMonthList.add(EmsStatisticalMonth.builder()
                .columnId(columnId)
                .val(String.valueOf(ep))
                .time(startDayTime)
                .build());
        String lastNum = String.valueOf(Double.parseDouble(currentNum)-ep);//上期示数
        //添加上期示数数据
        statisticalMonthList.add(EmsStatisticalMonth.builder()
                .columnId(columnId + ConfigDitEnums.NUM_LAST.getCode())
                .val(lastNum)
                .time(startDayTime)
                .build());
        if(!columnId.equals(ConfigColumnEnums.ENPT.getCode()) && !columnId.equals(ConfigColumnEnums.EPPT.getCode())){
            Double fee = ep * price;
            //添加费用数据
            statisticalMonthList.add(EmsStatisticalMonth.builder()
                    .columnId(columnId + ConfigDitEnums.FEE.getCode())
                    .val(KeepDecimalPointUtil.keepTwoPoint(fee))
                    .time(startDayTime)
                    .build());
            dataMap.put(columnId,fee);
        }
        if(price!=null) {
            statisticalMonthList.add(EmsStatisticalMonth.builder()
                    .columnId(columnId + ConfigDitEnums.PRICE.getCode())
                    .val(String.valueOf(price))
                    .time(startDayTime)
                    .build());
        }
        return InComeCalculationVO.builder().dataMap(dataMap).statisticalMonthList(statisticalMonthList).build();
    }
}
